const jwt = require("jsonwebtoken");
const { JWT_SECRET } = require("../../config/config.default");
const {
  createUser,
  getUserInfo,
  updateUserInfo,
  getUserList,
} = require("../../service/user/user.service");
const {
  userRegisterError,
  userLoginError,
  queryError,
} = require("../../constant/err.type");
const {
  querySuccess,
  registerSuccess,
  registerloginSuccess,
  updateSuccess,
} = require("../../constant/success.type");

class UserController {
  async register(ctx, nuxt) {
    console.log(ctx.request.body);
    try {
      const db_response = await createUser(ctx.request.body);
      ctx.body = registerSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", userLoginError, ctx);
    }
  }
  async login(ctx, nuxt) {
    const { phone_number } = ctx.request.body;
    try {
      const { password, ...result } = await getUserInfo({ phone_number });
      const token = jwt.sign(result, JWT_SECRET, {
        expiresIn: "1d",
      });
      registerloginSuccess.result.token = token;
      ctx.body = registerloginSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", userLoginError, ctx);
    }
  }
  async verifyCodeLogin(ctx, nuxt) {
    const { phone_number } = ctx.request.body;
    try {
      const { password, ...result } = await getUserInfo({ phone_number });
      const token = jwt.sign(result, JWT_SECRET, {
        expiresIn: "1d",
      });
      registerloginSuccess.result.token = token;
      ctx.body = registerloginSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", userRegisterError, ctx);
    }
  }
  async updataUserInfo(ctx, next) {
    try {
      const db_response = await updateUserInfo(ctx.request.body);
      updateSuccess.result = db_response;
      ctx.body = updateSuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", updateError, ctx);
    }
  }

  async getUserInfo(ctx, next) {
    const { id } = ctx.state.user;
    try {
      const respnse = await getUserInfo({ id });
      const ip_address = ctx.request.header['x-forwarded-for'] || ctx.ip || ctx.request.socket.remoteAddress; // 获取IP
      // 获取当前时间，记录登录时间
      const currentTime = new Date();
      // 加上时差8小时
      // const eightHoursInMilliseconds = 8 * 60 * 60 * 1000;
      // 得到本地时间
      // const last_login_time = new Date(currentTime.getTime() + eightHoursInMilliseconds);
      const last_login_time = new Date(currentTime.getTime());
      await updateUserInfo({id,ip_address,last_login_time});
      const { password, ...result } = respnse;
      querySuccess.result = result;
      ctx.body = querySuccess;
    } catch (err) {
      console.log(err);
      ctx.body = queryError;
    }
  }
  async getUserList(ctx, next) {
    try {
      const db_response = await getUserList(ctx.request.query);
      querySuccess.result = db_response;
      ctx.body = querySuccess;
    } catch (err) {
      console.log(err);
      ctx.app.emit("error", queryError, ctx);
    }
  }
}

module.exports = new UserController();
